/**
 * User: muxin4ever
 * Date: Oct, 31, 2012
 * Time: 21:25
 */

package gui;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;

import calculation.MatrixLibrary;

import common.MyButton;
import common.MyLabel;
import common.MyPanelRight;

public class PanelDisplayMatrixList extends MyPanelRight {
	private FrameMain f = null;

	private MyLabel label = new MyLabel(new ImageIcon("./image/panelDisplay2.png"));

	private JLayeredPane layeredPane = new JLayeredPane();
	private JScrollPane jsp;
	MyLabel p = null;
	ArrayList<MySet> al = null;

	public PanelDisplayMatrixList(FrameMain frameMain) {
		f = frameMain;
		p = new MyLabel(new ImageIcon("./image/frame1.png"));
		p.setBounds(53, 108, 408, 505);
		// p.setBackground(Color.black);
		// p.setSize(408, 505);
		jsp = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setBounds(53, 108, 408, 405);
		// jsp.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		// jsp.setOpaque(true);
		// jsp.setViewportView(p);
		label.setBounds(0, 0, 521, 600);

		layeredPane.setBounds(0, 0, 521, 600);
		layeredPane.add(label, new Integer(0));
		layeredPane.add(jsp, new Integer(1));

		this.add(layeredPane);

		al = new ArrayList<MySet>();

		String[] nameList = (String[]) MatrixLibrary.getNameList().toArray(new String[0]);

		setMyLayout(nameList);

	}

	public void setMyLayout(String[] nameList) {
		int i = 0;

		for (String name : nameList) {

			MyButton b1 = new MyButton(new ImageIcon("./image/Rename.png"));
			MyButton b2 = new MyButton(new ImageIcon("./image/Delete.png"));
			MyLabel label = new MyLabel(name);

			b1.setBounds(212, 20 + i * 30, 80, 31);
			b2.setBounds(300, 20 + i * 30, 80, 31);
			label.setBounds(20, 20 + i * 30, 200, 31);

			final String temp = name;
			MouseListener listener1 = new MouseListener() {
				public void mouseEntered(MouseEvent e) {
				}

				public void mouseClicked(MouseEvent arg0) {
					DialogRenameFile.setTarget(temp);
					f.getDialogRenameFile().setVisible(true);
				}

				public void mouseExited(MouseEvent arg0) {
				}

				public void mousePressed(MouseEvent arg0) {
				}

				public void mouseReleased(MouseEvent arg0) {
				}
			};
			b1.addMouseListener(listener1);

			MouseListener listener2 = new MouseListener() {
				public void mouseEntered(MouseEvent e) {
				}

				public void mouseClicked(MouseEvent arg0) {
					DialogDeleteFile.setTarget(temp);
					f.getDialogDeleteFile().setVisible(true);
				}

				public void mouseExited(MouseEvent arg0) {
				}

				public void mousePressed(MouseEvent arg0) {
				}

				public void mouseReleased(MouseEvent arg0) {
				}
			};
			b2.addMouseListener(listener2);

			// al.add(new MySet(name,b1,b2,label,listener1,listener2));
			p.add(label);
			p.add(b1);
			p.add(b2);
			i++;
		}

	}

	public void updateMyLayout(String[] nameList) {
		int i = 0;
		p.removeAll();
		for (String name : nameList) {

			MyButton b1 = new MyButton(new ImageIcon("./image/Rename.png"));
			MyButton b2 = new MyButton(new ImageIcon("./image/Delete.png"));
			MyLabel label = new MyLabel(name);

			b1.setBounds(212, 20 + i * 30, 80, 31);
			b2.setBounds(300, 20 + i * 30, 80, 31);
			label.setBounds(20, 20 + i * 30, 200, 31);

			final String temp = name;
			MouseListener listener1 = new MouseListener() {
				public void mouseEntered(MouseEvent e) {
				}

				public void mouseClicked(MouseEvent arg0) {
					DialogRenameFile.setTarget(temp);
					f.getDialogRenameFile().setVisible(true);
				}

				public void mouseExited(MouseEvent arg0) {
				}

				public void mousePressed(MouseEvent arg0) {
				}

				public void mouseReleased(MouseEvent arg0) {
				}
			};
			b1.addMouseListener(listener1);

			MouseListener listener2 = new MouseListener() {
				public void mouseEntered(MouseEvent e) {
				}

				public void mouseClicked(MouseEvent arg0) {
					DialogDeleteFile.setTarget(temp);
					f.getDialogDeleteFile().setVisible(true);
				}

				public void mouseExited(MouseEvent arg0) {
				}

				public void mousePressed(MouseEvent arg0) {
				}

				public void mouseReleased(MouseEvent arg0) {
				}
			};
			b2.addMouseListener(listener2);

			// al.add(new MySet(name,b1,b2,label,listener1,listener2));
			p.add(label);
			p.add(b1);
			p.add(b2);
			i++;
		}

	}
}

class MySet {
	String str;
	MyButton b1;
	MyButton b2;
	MyLabel label;
	MouseListener b1e;
	MouseListener b2e;

	MySet(String str, MyButton b1, MyButton b2, MyLabel label, MouseListener b1e, MouseListener b2e) {
		this.str = str;
		this.b1 = b1;
		this.b2 = b2;
		this.label = label;
		this.b1e = b1e;
		this.b2e = b2e;
	}

	public MyLabel getLabel() {
		return this.label;
	}

	public MyButton getB1() {
		return this.b1;
	}

	public MyButton getB2() {
		return this.b2;
	}
}
